import { ISystemConfig } from "@/types/config";
import mongoose from 'mongoose'

const prodConfig: ISystemConfig = {
    pubImagePath: '/var/baby/images',
    pubVideoPath: '/var/baby/videos',
    database: {
        scheme: 'mongodb',
        dbhost: '127.0.0.1',
        dbtype: 'mongo',
        dbport: '27017',
        db: 'babyhapi',
        username: 'stone',
        password: 'Sd&Lq4760699',
    },
    port: 9001
}

const devConfig: ISystemConfig = {
    pubImagePath: '/var/baby/images',
    pubVideoPath: '/var/baby/videos',
    database: {
        dbhost: '139.198.190.76',
        scheme: 'mongodb',
        dbtype: 'mongo',
        dbport: '27017',
        db: 'babyhapi',
        username: 'stone',
        password: 'Sd&Lq4760699',
    },
    port: 9001
}

const systemConfig: ISystemConfig = prodConfig

class Database {
    static async connect() {
        let mongoUrl =
            `${systemConfig.database.scheme}://${systemConfig.database.username}:`+
            `${systemConfig.database.password}@${systemConfig.database.dbhost}:`+
            `${systemConfig.database.dbport}/${systemConfig.database.db}`
        console.log(mongoUrl)
        mongoose.set('useCreateIndex', true)
        let connection = mongoose.connection
        connection.on('error', console.error.bind(console, 'error: '))
        connection.once('open', function (){
            console.log('mongo connect success!')
        })
        await mongoose.connect(mongoUrl, {
            useNewUrlParser: true,
            useUnifiedTopology: true
        })

    }
}

export {
    systemConfig,
    Database
}
